using System.ComponentModel;

namespace DocumentManager.Models;

/// <summary>
/// 文档模型
/// </summary>
public class Document : INotifyPropertyChanged
{
    private string _title = "新建文档";
    private string _author = Environment.UserName;
    private string _description = string.Empty;
    private string _filePath = string.Empty;
    private bool _hasUnsavedChanges;

    /// <summary>
    /// 文档ID
    /// </summary>
    public string Id { get; set; } = Guid.NewGuid().ToString();

    /// <summary>
    /// 文档标题
    /// </summary>
    public string Title
    {
        get => _title;
        set
        {
            if (_title != value)
            {
                _title = value;
                OnPropertyChanged(nameof(Title));
                HasUnsavedChanges = true;
            }
        }
    }

    /// <summary>
    /// 作者
    /// </summary>
    public string Author
    {
        get => _author;
        set
        {
            if (_author != value)
            {
                _author = value;
                OnPropertyChanged(nameof(Author));
                HasUnsavedChanges = true;
            }
        }
    }

    /// <summary>
    /// 描述
    /// </summary>
    public string Description
    {
        get => _description;
        set
        {
            if (_description != value)
            {
                _description = value;
                OnPropertyChanged(nameof(Description));
                HasUnsavedChanges = true;
            }
        }
    }

    /// <summary>
    /// 文件路径
    /// </summary>
    public string FilePath
    {
        get => _filePath;
        set
        {
            if (_filePath != value)
            {
                _filePath = value;
                OnPropertyChanged(nameof(FilePath));
            }
        }
    }

    /// <summary>
    /// 是否有未保存的更改
    /// </summary>
    public bool HasUnsavedChanges
    {
        get => _hasUnsavedChanges;
        set
        {
            if (_hasUnsavedChanges != value)
            {
                _hasUnsavedChanges = value;
                OnPropertyChanged(nameof(HasUnsavedChanges));
            }
        }
    }

    /// <summary>
    /// 根节点
    /// </summary>
    public DocumentNode RootNode { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreatedTime { get; set; } = DateTime.Now;

    /// <summary>
    /// 修改时间
    /// </summary>
    public DateTime ModifiedTime { get; set; } = DateTime.Now;

    /// <summary>
    /// 版本号
    /// </summary>
    public string Version { get; set; } = "1.0.0";

    public event PropertyChangedEventHandler? PropertyChanged;

    public Document()
    {
        // 初始化根节点
        RootNode = new DocumentNode
        {
            Title = Title,
            NodeType = DocumentNodeType.Document,
            Level = 0
        };

        // 监听根节点变化
        RootNode.PropertyChanged += (s, e) => HasUnsavedChanges = true;
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        if (propertyName != nameof(ModifiedTime))
        {
            ModifiedTime = DateTime.Now;
        }
    }

    /// <summary>
    /// 获取所有节点
    /// </summary>
    public IEnumerable<DocumentNode> GetAllNodes()
    {
        yield return RootNode;
        foreach (var node in RootNode.GetAllDescendants())
        {
            yield return node;
        }
    }

    /// <summary>
    /// 根据ID查找节点
    /// </summary>
    public DocumentNode? FindNodeById(string id)
    {
        if (RootNode.Id == id)
            return RootNode;

        return RootNode.GetAllDescendants().FirstOrDefault(n => n.Id == id);
    }

    /// <summary>
    /// 获取统计信息
    /// </summary>
    public DocumentStatistics GetStatistics()
    {
        var allNodes = GetAllNodes().ToList();
        var textContents = allNodes.SelectMany(n => n.Contents.OfType<TextContentItem>()).ToList();
        var imageContents = allNodes.SelectMany(n => n.Contents.OfType<ImageContentItem>()).ToList();

        return new DocumentStatistics
        {
            TotalNodes = allNodes.Count,
            TitleNodes = allNodes.Count(n => n.NodeType == DocumentNodeType.Title),
            TextContents = textContents.Count,
            ImageContents = imageContents.Count,
            TotalCharacters = textContents.Sum(t => t.Text.Length),
            TotalWords = textContents.Sum(t => CountWords(t.Text))
        };
    }

    private static int CountWords(string text)
    {
        if (string.IsNullOrWhiteSpace(text))
            return 0;

        return text.Split(new[] { ' ', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries).Length;
    }
}

/// <summary>
/// 文档统计信息
/// </summary>
public class DocumentStatistics
{
    public int TotalNodes { get; set; }
    public int TitleNodes { get; set; }
    public int TextContents { get; set; }
    public int ImageContents { get; set; }
    public int TotalCharacters { get; set; }
    public int TotalWords { get; set; }
} 